/**
 * 作者: wgx
 * 日期: 2024-12-23 20:25:32
 * 备注: 玩家物资争夺被打防守记录
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserSnatchRecord;
import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.logic.CaveManager;
import com.motu.monstercity.server.game.logic.FightManager;
import com.motu.monstercity.server.game.logic.GameUser;
import com.motu.vertx.module.utility.model.EntityManager;
import com.motu.vertx.module.utility.redis.RedisTools;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;

public class UserSnatchRecord extends UserSnatchRecordModel {
    public UserSnatchRecord() {
        super();
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public JsonObject insert() {
        // 如果超过上限则会删除最早的那条
        long count = Future.await(RedisTools.getHashLen(getListKey()));
        if (count >= AllParam.SNATCH_FIGHT_RECORD_MAX) {
            String orderListKey = EntityManager.getOrderListKey(getListKey());
            String delRecord = Future.await(RedisTools.lpop(orderListKey));// 删除列表数据
            long id = Long.parseLong(delRecord);// dbid
            UserSnatchRecord record = FightManager.getUserSnatchRecord(getUserId(), id);
            if (record != null) {
                record.delete();
            }
        }
        return super.insert();
    }

    public PBUserSnatchRecord.Builder takeInitBuilder() {
        PBUserSnatchRecord.Builder sb = super.takeInitBuilder();
        UserShow userShow = GameUser.getUserShow(getEnemyId());
        if (userShow != null) {
            sb.setEnemyInfo(userShow.takeInitBuilder());
        }
        return sb;
    }
}
